package com.xy.biz.uc.domain.converter;

import com.xy.biz.uc.domain.bo.RoleBO;
import com.xy.biz.uc.domain.bo.UserGroupBO;
import com.xy.biz.uc.domain.dto.UserGroupDTO;
import com.xy.biz.uc.domain.po.UserGroupPO;
import com.xy.biz.uc.domain.query.UserGroupQuery;
import com.xy.biz.uc.domain.req.ListUserGroupRequest;
import com.xy.biz.uc.domain.req.SaveUserGroupRequest;
import com.xy.biz.uc.domain.resp.GetUserGroupResponse;
import com.xy.lang.domain.LongIdCodeName;
import com.xy.tool.util.BeanUtilsEx;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * 
 * 
 * @author zsp
 * @date 2020-8-20
 */
public class UserGroupConverter {
	
	public static UserGroupPO toPO(UserGroupBO source) {
		return BeanUtilsEx.copyProperties(source, () -> new UserGroupPO(), true);
	}
	
	public static List<UserGroupPO> toPO(Collection<UserGroupBO> source) {
		return BeanUtilsEx.copyPropertiesForNewList(source, () -> new UserGroupPO(), true);
	}
	
	public static UserGroupBO fromPO(UserGroupPO source) {
		return BeanUtilsEx.copyProperties(source, () -> new UserGroupBO());
	}
	
	public static List<UserGroupBO> fromPO(Collection<UserGroupPO> source) {
		return BeanUtilsEx.copyPropertiesForNewList(source, () -> new UserGroupBO());
	}
	
	public static UserGroupBO toBO(SaveUserGroupRequest source) {
		return BeanUtilsEx.copyProperties(source, () -> new UserGroupBO(), true);
	}
	
	public static List<GetUserGroupResponse> toResponse(Collection<UserGroupBO> source) {
		return BeanUtilsEx.copyPropertiesForNewList(source, GetUserGroupResponse::new, true);
	}
	public static GetUserGroupResponse toResponse(UserGroupBO source) {
		return BeanUtilsEx.copyProperties(source, GetUserGroupResponse::new, true);
	}

	public static List<UserGroupBO> toBO(Collection<SaveUserGroupRequest> source) {
		return BeanUtilsEx.copyPropertiesForNewList(source, () -> new UserGroupBO(), true);
	}
	
	public static UserGroupDTO toDTO(UserGroupBO source) {
		UserGroupDTO target = new UserGroupDTO();
		BeanUtilsEx.copyProperties(source, target);
		buildDTO(source, target);
		return target;
	}
	
	public static List<UserGroupDTO> toDTO(Collection<UserGroupBO> source) {
		return BeanUtilsEx.copyPropertiesForNewList(source, () -> new UserGroupDTO(), (s, t) -> buildDTO(s, t));
	}

	private static void buildDTO(UserGroupBO source, UserGroupDTO target) {
		LongIdCodeName longIdCodeName;
		Set<RoleBO> roleBOSet = source.getRoleBOSet();
		if(!CollectionUtils.isEmpty(roleBOSet)) {
			List<LongIdCodeName> roleList = new ArrayList<>();
			for(RoleBO roleBO : roleBOSet) {
				longIdCodeName = new LongIdCodeName();
				longIdCodeName.setId(roleBO.getRoleId());
				longIdCodeName.setCode(roleBO.getRoleCode());
				longIdCodeName.setName(roleBO.getName());
				roleList.add(longIdCodeName);
			}
			target.setRoleList(roleList);
		}
	}
	
	public static UserGroupQuery toQuery(ListUserGroupRequest source) {
		if(null == source) {
			return null;
		}
		UserGroupQuery target = new UserGroupQuery(source.getPageIndex(), source.getPageSize());
		BeanUtilsEx.copyProperties(source, target);
		return target;
	}

}
